package cm.iccsoft.android.telecomcost.ui;

import java.util.ArrayList;
import java.util.Calendar;

import cm.iccsoft.android.telecomcost.DatabaseManager;
import cm.iccsoft.android.telecomcost.R;
import cm.iccsoft.android.telecomcost.TelecomCost;
import cm.iccsoft.android.telecomcost.activities.MonthlyThresholdListActivity;
import cm.iccsoft.android.telecomcost.entities.MonthlyThreshold;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class MonthlyThresholdDialog extends Dialog {

	private DatabaseManager db;
	private TextView lblJan;
	private TextView lblFeb;
	private TextView lblMar;
	private TextView lblAvr;
	private TextView lblMai;
	private TextView lblJun;
	private TextView lblJul;
	private TextView lblAug;
	private TextView lblSep;
	private TextView lblOct;
	private TextView lblNov;
	private TextView lblDec;
	private EditText txtJan;
	private EditText txtFeb;
	private EditText txtMar;
	private EditText txtApr;
	private EditText txtMai;
	private EditText txtJun;
	private EditText txtJul;
	private EditText txtAug;
	private EditText txtSep;
	private EditText txtOct;
	private EditText txtNov;
	private EditText txtDec;

	private Button btnNext;
	private Button btnPrev;
	private Button btnClose;
	private Button btnSave;

	private static final int JAN_MAR_VIEW = 0;
	private static final int AVR_JUN_VIEW = 1;
	private static final int JUL_SEP_VIEW = 2;
	private static final int OCT_DEC_VIEW = 3;

	private CharSequence JAN_MAR_LABEL = DateUtils.getMonthString(
			Calendar.JANUARY, DateUtils.LENGTH_SHORT)
			+ " - "
			+ DateUtils.getMonthString(Calendar.MARCH, DateUtils.LENGTH_SHORT);
	private CharSequence AVR_JUN_LABEL = DateUtils.getMonthString(
			Calendar.APRIL, DateUtils.LENGTH_SHORT)
			+ " - "
			+ DateUtils.getMonthString(Calendar.JUNE, DateUtils.LENGTH_SHORT);
	private CharSequence JUL_SEP_LABEL = DateUtils.getMonthString(
			Calendar.JULY, DateUtils.LENGTH_SHORT)
			+ " - "
			+ DateUtils.getMonthString(Calendar.SEPTEMBER,
					DateUtils.LENGTH_SHORT);
	private CharSequence OCT_DEC_LABEL = DateUtils.getMonthString(
			Calendar.OCTOBER, DateUtils.LENGTH_SHORT)
			+ " - "
			+ DateUtils.getMonthString(Calendar.DECEMBER,
					DateUtils.LENGTH_SHORT);

	private int viewIndex = 0;

	private boolean shouldRefreshThresholdList = false;
	private ViewFlipper flipper;
	private Context _context;

	/**
	 * @param context
	 * @param cancelable
	 * @param cancelListener
	 */
	public MonthlyThresholdDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		_context = context;
	}

	/**
	 * @param context
	 * @param theme
	 */
	public MonthlyThresholdDialog(Context context, int theme) {
		super(context, theme);
		_context = context;
	}

	public MonthlyThresholdDialog(Context context) {
		super(context);
		_context = context;
	}

	private void saveMonthlyThreshold() throws Exception {
		Log.i("MonthlyThresholdDialog", "saving MonthlyThreshold...");

		MonthlyThresholdListActivity activity = (MonthlyThresholdListActivity) _context;
		ArrayList<MonthlyThreshold> thresholds = activity.getThresholds();

		Log.i("MonthlyThresholdDialog", "we load the MonthlyThreshold first...");

		MonthlyThreshold threshold = db.getMonthlyThreshold(Calendar.JANUARY,
				thresholds);
		threshold.setAmount(txtJan.getText().toString().equals("") ? 0 : Double
				.parseDouble(txtJan.getText().toString()));
		activity.setMonthlyThresgold(db.saveMonthlyThreshold(threshold));

		threshold = db.getMonthlyThreshold(Calendar.FEBRUARY, thresholds);
		threshold.setAmount(txtFeb.getText().toString().equals("") ? 0 : Double
				.parseDouble(txtFeb.getText().toString()));
		activity.setMonthlyThresgold(db.saveMonthlyThreshold(threshold));

		threshold = db.getMonthlyThreshold(Calendar.MARCH, thresholds);
		threshold.setAmount(txtMar.getText().toString().equals("") ? 0 : Double
				.parseDouble(txtMar.getText().toString()));
		activity.setMonthlyThresgold(db.saveMonthlyThreshold(threshold));

		threshold = db.getMonthlyThreshold(Calendar.APRIL, thresholds);
		threshold.setAmount(txtMar.getText().toString().equals("") ? 0 : Double
				.parseDouble(txtMar.getText().toString()));
		activity.setMonthlyThresgold(db.saveMonthlyThreshold(threshold));

		threshold = db.getMonthlyThreshold(Calendar.MAY, thresholds);
		threshold.setAmount(txtMai.getText().toString().equals("") ? 0 : Double
				.parseDouble(txtMai.getText().toString()));
		activity.setMonthlyThresgold(db.saveMonthlyThreshold(threshold));

		threshold = db.getMonthlyThreshold(Calendar.JUNE, thresholds);
		threshold.setAmount(txtJun.getText().toString().equals("") ? 0 : Double
				.parseDouble(txtJun.getText().toString()));
		activity.setMonthlyThresgold(db.saveMonthlyThreshold(threshold));

		threshold = db.getMonthlyThreshold(Calendar.JULY, thresholds);
		threshold.setAmount(txtJul.getText().toString().equals("") ? 0 : Double
				.parseDouble(txtJul.getText().toString()));
		activity.setMonthlyThresgold(db.saveMonthlyThreshold(threshold));

		threshold = db.getMonthlyThreshold(Calendar.AUGUST, thresholds);
		threshold.setAmount(txtAug.getText().toString().equals("") ? 0 : Double
				.parseDouble(txtAug.getText().toString()));
		activity.setMonthlyThresgold(db.saveMonthlyThreshold(threshold));

		threshold = db.getMonthlyThreshold(
				Calendar.SEPTEMBER, thresholds);
		threshold
				.setAmount(txtSep.getText().toString().equals("") ? 0 : Double
						.parseDouble(txtSep.getText().toString()));
		activity.setMonthlyThresgold(db.saveMonthlyThreshold(threshold));

		threshold = db.getMonthlyThreshold(
				Calendar.OCTOBER, thresholds);
		threshold.setAmount(txtOct.getText().toString().equals("") ? 0
				: Double.parseDouble(txtOct.getText().toString()));
		activity.setMonthlyThresgold(db.saveMonthlyThreshold(threshold));
		
		threshold = db.getMonthlyThreshold(
				Calendar.NOVEMBER, thresholds);
		threshold.setAmount(txtNov.getText().toString().equals("") ? 0
				: Double.parseDouble(txtNov.getText().toString()));
		activity.setMonthlyThresgold(db.saveMonthlyThreshold(threshold));
		
		threshold = db.getMonthlyThreshold(
				Calendar.DECEMBER, thresholds);				
		threshold.setAmount(txtDec.getText().toString().equals("") ? 0
				: Double.parseDouble(txtDec.getText().toString()));
		activity.setMonthlyThresgold(db.saveMonthlyThreshold(threshold));

		shouldRefreshThresholdList = true;
		Log.i("MonthlyThresholdDialog", "monthly threshold saved...");
		Toast.makeText(_context, "Threshold saved !!!", 3).show();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("MonthlyThresholdDialog", "Creating the dialog...");
		super.onCreate(savedInstanceState);

		db = TelecomCost.getDatabaseManager();
		setContentView(R.layout.monthly_threshold_dialog);

		setTitle("Monthly Threshold");

		flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
		lblJan = (TextView) findViewById(R.id.lblJan);
		lblFeb = (TextView) findViewById(R.id.lblFeb);
		lblMar = (TextView) findViewById(R.id.lblMar);
		lblAvr = (TextView) findViewById(R.id.lblAvr);
		lblMai = (TextView) findViewById(R.id.lblMai);
		lblJun = (TextView) findViewById(R.id.lblJun);
		lblJul = (TextView) findViewById(R.id.lblJul);
		lblAug = (TextView) findViewById(R.id.lblAug);
		lblSep = (TextView) findViewById(R.id.lblSep);
		lblOct = (TextView) findViewById(R.id.lblOct);
		lblNov = (TextView) findViewById(R.id.lblNov);
		lblDec = (TextView) findViewById(R.id.lblDec);

		txtJan = (EditText) findViewById(R.id.txtJan);
		txtFeb = (EditText) findViewById(R.id.txtFeb);
		txtMar = (EditText) findViewById(R.id.txtMar);
		txtApr = (EditText) findViewById(R.id.txtAvr);
		txtMai = (EditText) findViewById(R.id.txtMai);
		txtJun = (EditText) findViewById(R.id.txtJun);
		txtJul = (EditText) findViewById(R.id.txtJul);
		txtAug = (EditText) findViewById(R.id.txtAug);
		txtSep = (EditText) findViewById(R.id.txtSep);
		txtOct = (EditText) findViewById(R.id.txtOct);
		txtNov = (EditText) findViewById(R.id.txtNov);
		txtDec = (EditText) findViewById(R.id.txtDec);

		btnClose = (Button) findViewById(R.id.btnCloseMonthlyThresholdDialog);
		btnSave = (Button) findViewById(R.id.btnSaveMonthlyThresholdDialog);
		btnNext = (Button) findViewById(R.id.btnNext);
		btnPrev = (Button) findViewById(R.id.btnPrevious);

		lblJan.setText(DateUtils.getMonthString(Calendar.JANUARY,
				DateUtils.LENGTH_LONG));
		lblFeb.setText(DateUtils.getMonthString(Calendar.FEBRUARY,
				DateUtils.LENGTH_LONG));
		lblMar.setText(DateUtils.getMonthString(Calendar.MARCH,
				DateUtils.LENGTH_LONG));
		lblAvr.setText(DateUtils.getMonthString(Calendar.APRIL,
				DateUtils.LENGTH_LONG));
		lblMai.setText(DateUtils.getMonthString(Calendar.MAY,
				DateUtils.LENGTH_LONG));
		lblJun.setText(DateUtils.getMonthString(Calendar.JUNE,
				DateUtils.LENGTH_LONG));
		lblJul.setText(DateUtils.getMonthString(Calendar.JULY,
				DateUtils.LENGTH_LONG));
		lblAug.setText(DateUtils.getMonthString(Calendar.AUGUST,
				DateUtils.LENGTH_LONG));
		lblSep.setText(DateUtils.getMonthString(Calendar.SEPTEMBER,
				DateUtils.LENGTH_LONG));
		lblOct.setText(DateUtils.getMonthString(Calendar.OCTOBER,
				DateUtils.LENGTH_LONG));
		lblNov.setText(DateUtils.getMonthString(Calendar.NOVEMBER,
				DateUtils.LENGTH_LONG));
		lblDec.setText(DateUtils.getMonthString(Calendar.DECEMBER,
				DateUtils.LENGTH_LONG));

		setButtons();

		btnNext.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				showNextView();
			}
		});

		btnPrev.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				showPreviousView();
			}
		});

		btnClose.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				closeDialog();
			}
		});

		btnSave.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				try {
					saveMonthlyThreshold();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		Log.i("MonthlyThresholdDialog", "Dialog ohne bundle created....");
	}

	private void setButtons() {
		switch (viewIndex) {
		case JAN_MAR_VIEW:
			btnPrev.setEnabled(false);
			btnNext.setEnabled(true);
			btnNext.setText(AVR_JUN_LABEL);
			btnPrev.setText("<<");
			break;
		case AVR_JUN_VIEW:
			btnPrev.setEnabled(true);
			btnNext.setEnabled(true);
			btnNext.setText(JUL_SEP_LABEL);
			btnPrev.setText(JAN_MAR_LABEL);
			break;
		case JUL_SEP_VIEW:
			btnPrev.setEnabled(true);
			btnNext.setEnabled(true);
			btnNext.setText(OCT_DEC_LABEL);
			btnPrev.setText(AVR_JUN_LABEL);
			break;
		case OCT_DEC_VIEW:
			btnPrev.setEnabled(true);
			btnNext.setEnabled(false);
			btnNext.setText(">>");
			btnPrev.setText(JUL_SEP_LABEL);
			break;
		default:
			Log.i("MonthlyThresholdDialog", "invalid viewIndexx...."
					+ viewIndex);
			break;
		}
	}

	protected void showPreviousView() {
		if (viewIndex > JAN_MAR_VIEW) {
			viewIndex--;
			flipper.setDisplayedChild(viewIndex);
		}

		setButtons();
	}

	protected void showNextView() {
		if (viewIndex < OCT_DEC_VIEW) {
			viewIndex++;
			flipper.setDisplayedChild(viewIndex);
		}

		setButtons();
	}

	protected void closeDialog() {
		if (shouldRefreshThresholdList && _context != null) {
			((MonthlyThresholdListActivity) _context).bindThresholds();
		}
		if (viewIndex == 1) {
			viewIndex = 0;
			flipper.setDisplayedChild(JAN_MAR_VIEW);
		}

		hide();
	}

	protected void showThursdaySundayView() {
		viewIndex++;
		flipper.showNext();
	}

	protected void showMondayWednesdayView() {
		viewIndex--;
		flipper.showPrevious();
	}

	@Override
	public void show() {
		Log.i("MonthlyThresholdDialog", "showing the dialog...");
		super.show();
		Log.i("MonthlyThresholdDialog", "dialog on screen...");
		bindMonthlyTreshold();
	}

	@Override
	protected void onStart() {
		Log.i("MonthlyThresholdDialog", "Starting the dialog...");
		super.onStart();
		Log.i("MonthlyThresholdDialog", "dialog started...");
	}

	private void bindMonthlyTreshold() {
		Log.i("MonthlyThresholdDialog", "binding MonthlyThresholdDialog...");

		MonthlyThresholdListActivity activity = (MonthlyThresholdListActivity) _context;
		ArrayList<MonthlyThreshold> thresholds = activity.getThresholds();

		MonthlyThreshold threshold = db.getMonthlyThreshold(Calendar.JANUARY,
				thresholds);
		txtJan.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getMonthlyThreshold(Calendar.FEBRUARY, thresholds);
		txtFeb.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getMonthlyThreshold(Calendar.MARCH, thresholds);
		txtMar.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getMonthlyThreshold(Calendar.APRIL, thresholds);
		txtApr.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getMonthlyThreshold(Calendar.MAY, thresholds);
		txtMai.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getMonthlyThreshold(Calendar.JUNE, thresholds);
		txtJun.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getMonthlyThreshold(Calendar.JULY, thresholds);
		txtJul.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getMonthlyThreshold(Calendar.AUGUST, thresholds);
		txtAug.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getMonthlyThreshold(Calendar.SEPTEMBER, thresholds);
		txtSep.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getMonthlyThreshold(Calendar.OCTOBER, thresholds);
		txtOct.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getMonthlyThreshold(Calendar.NOVEMBER, thresholds);
		txtNov.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getMonthlyThreshold(Calendar.DECEMBER, thresholds);
		txtDec.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));
	}

	@Override
	protected void onStop() {
		Log.i("MonthlyThresholdDialog", "Stopping the dialog...");

		if (shouldRefreshThresholdList && _context != null) {
			((MonthlyThresholdListActivity) _context).bindThresholds();
		}

		super.onStop();
		Log.i("MonthlyThresholdDialog", "dialog stopped...");
	}
}
